package org.sixtynine.stock.dao.impl;

/**
 * @author Thilan
 *
 */

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.sixtynine.stock.Constants;
import org.sixtynine.stock.dao.WeeklySectorDataDao;
import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.WeeklySectorData;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class WeeklySectorDataDaoImpl extends
		AbstractDaoImpl<WeeklySectorData, Integer> implements
		WeeklySectorDataDao {

	protected WeeklySectorDataDaoImpl() {
		super(WeeklySectorData.class);
	}

	@Override
	public void saveWeeklySectorData(WeeklySectorData monthlySectorData) {
		saveOrUpdate(monthlySectorData);
	}

	@Override
	public List<WeeklySectorData> findWeeklySectorData(int sectorid) {
		return findByCriteria(Restrictions.eq("sector.id", sectorid));
	}

	@Override
	public List<WeeklySectorData> searchWeeklySectorData(int sectorId,
			Date fromDate, Date toDate, String orderBy, String orderByName) {
		Criteria criteria = getCurrentSession().createCriteria(
				WeeklySectorData.class);

		criteria.addOrder(Order.asc(getRecordOrder(orderByName)));
		criteria.addOrder(Order.desc(getRecordOrder(orderBy)));

		if (sectorId > Constants.ERROR) {
			criteria.add(Restrictions.eq("sector.id", sectorId));
		}
		if (fromDate != null) {
			criteria.add(Restrictions.ge("date", fromDate));
		}

		if (toDate != null) {
			criteria.add(Restrictions.le("date", toDate));
		}

		return findByCriteria(criteria);
	}

	@Override
	public Page<WeeklySectorData> searchWeeklySectorData(int sectorId,
			Date fromDate, Date toDate, String orderBy, String orderByName,
			GridPramDTO gridPramDTO) {
		Criteria criteria = getCurrentSession().createCriteria(
				WeeklySectorData.class);

		criteria.addOrder(Order.asc(getRecordOrder(orderByName)));
		criteria.addOrder(Order.desc(getRecordOrder(orderBy)));

		if (sectorId > Constants.ERROR) {
			criteria.add(Restrictions.eq("sector.id", sectorId));
		}
		if (fromDate != null) {
			criteria.add(Restrictions.ge("date", fromDate));
		}

		if (toDate != null) {
			criteria.add(Restrictions.le("date", toDate));
		}

		return findByCriteria(criteria, gridPramDTO);
	}

	@Override
	@Transactional
	public void deleteWeeklySectorData(int sectorId, Date fromDate, Date toDate) {
		String sql = "delete from WeeklySectorData where 1=1 ";
		if (sectorId > Constants.ERROR) {
			sql += "and sector_id = :sectorId ";
		}
		if (fromDate != null) {
			sql += "and date >= :fromData ";
		}
		if (toDate != null) {
			sql += "and date <= :toDate ";
		}

		Query query = getCurrentSession().createQuery(sql);
		if (sectorId > Constants.ERROR) {
			query.setInteger("sectorId", sectorId);
		}
		if (fromDate != null) {
			query.setParameter("fromDate", fromDate);
		}
		if (toDate != null) {
			query.setParameter("toDate", toDate);
		}

		query.executeUpdate();

	}

}
